/**
 * Author：Sam Liu
 * Date: 21.7.26
 * Description: sku数据结构转换
 */
 import { replaceCDN } from "@xiaoe/js-tools/lib/replaceCDN";

// sku 属性格式转换，后端格式转 vant-sku 组件格式
export const skuPropertyConvert = (skuProperty) => {
  return skuProperty.map(e => {
    return {
      k: e.attr_name,
      k_s: e.attr_name,
      v: ((e) => {
        return e.value_arr.map(d => {
          return {
            id: d.attr_value,
            name: d.attr_value,
            imgUrl: replaceCDN(d.attr_img, {width: "600"})
          }
        })
      })(e)
    }
  })
}

// sku 格式转换，后端格式转 vant-sku 组件格式
export const skuListConvert = (skuList) => {
  return skuList.map(e => {
    let obj = {
      id: e.sku_id,
      price: e.sku_price,
      stock_num: e.sku_stock.left_num
    }
    for (let item of e.attr_value) {
      obj[item.attr_name] = item.attr_value
    }
    return obj
  })
}

// 算总库存
export const stockAmountCount = (skuList) => {
  return skuList.reduce((m,n) => {
    return m + n.sku_stock.left_num
  }, 0)
}

// 营销活动更新sku价格
export function marketingSkuListConvert (marketingData, sku) {
  // 更新sku list
  let {specs_info, activity_type} = marketingData
  let {list} = sku
  let secKillStock = 0
  list.forEach(e => {
    specs_info.forEach(d => {
      if (d.specs_id === e.id) {
        e.price = d.price
        // 秒杀处理库存 d.stock_num === 0 代表秒杀不限量，此时用商品库存
        if (activity_type === 8) {
          e.stock_num = d.stock === 0 ? e.stock_num : d.now_stock
          secKillStock = secKillStock + e.stock_num
        }
      }
    })
  })
  if (activity_type === 8) {
    sku.stock_num = secKillStock
  }

  // 更新价格
  let {low_price, high_price} = marketingData
  sku.price = low_price === high_price ?
              centToYuan(low_price) : `${ centToYuan(low_price)} - ${centToYuan(high_price)}`
}

// 超级会员更新sku价格
export function svipListConvert (vipPrice, sku) {
  let {list} = sku

  list.forEach(e => {
    e.price = vipPrice[e.id]
  })
  let priceArr = Object.values(vipPrice)
  let low_price = Math.min(...priceArr)
  let high_price = Math.max(...priceArr)
  sku.price = low_price === high_price ?
    centToYuan(low_price) : `${ centToYuan(low_price)} - ${centToYuan(high_price)}`
}

// 秒杀活动是否正常开始
export function isSeckillStart(data) {
  const {activity_type, is_subscribe, in_activity, user_subscribed, event_has_stock} = data

  // 不是秒杀一律返回true
  if (activity_type !== 8) return true

  // 需要预约、在活动中、用户已预约、有库存
  let needSubscribe = is_subscribe === 1 && in_activity === 1 && user_subscribed === 1 && event_has_stock === 1
  // 在活动中，有库存
  let notNeedSubscribe = is_subscribe === 0 && in_activity === 1 && event_has_stock === 1

  // 有两种情况，满足二者中一个就行
  return notNeedSubscribe || needSubscribe
}

// 分转换为元
export function centToYuan(num) {
  return (num / 100).toFixed(2)
}

// 专用于superVipMarketingCompare 价格计算的函数
export function getMinimumPrice(data, type) {
  let priceArr = []
  if (type === 'marketing') {
    priceArr = data.map(e => {return e.price})
  }
  if (type === 'svip') {
    priceArr = Object.values(data)
  }
  return Math.min(...priceArr)
}

// 判断是否空对象
export const isEmptyObject = (object) => {
  return JSON.stringify(object) === "{}";
};
